package net.lab1024.sa.admin.module.system.sms;


import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.extra.tokenizer.Result;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping
public class SmsController {

    @Autowired
    private SmsCodeService smsCodeService; // 业务逻辑层

    @PostMapping("/send")
    public ResponseDTO<String> sendCode(@RequestParam String mobile) {
        // 1. 校验手机号格式
        if (!PhoneUtil.isMobile(mobile)) {
            return ResponseDTO.userErrorParam("手机号格式不正确！");
        }
//        // 2. 校验发送频率（防止短信轰炸）
//        if (smsCodeService.checkSendFrequency(mobile)) {
//            return ResponseDTO.userErrorParam("发送过于频繁，请稍后再试");
//        }
        // 3. 生成验证码
        String code = smsCodeService.generateCode();
        // 4. 存储验证码到Redis
        smsCodeService.saveCodeToRedis(mobile, code);
//        // 5. 调用短信代理发送（通过工厂获取具体实现）
//        SmsSender smsSender = smsCodeService.getSmsSender(); // 可配置默认服务商
//        boolean sendResult = smsSender.send(mobile, code);
        if (false) {
            return ResponseDTO.userErrorParam("短信发送失败，请稍后再试");
        }
        return ResponseDTO.okMsg("验证码已发送");
    }
}
